﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Modules
{
    public partial class ThuTrongNgay : DevExpress.XtraEditors.XtraForm
    {
        String sErr = "";
        DTO.GIAODICHInfo inf = new DTO.GIAODICHInfo();
        int idThem = 37;
        void SetPermission()
        {
            if (this.Parent.Parent is frmMain)
            {
                frmMain main = (frmMain)this.Parent.Parent;
                btnThem.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION);
                btnLuu.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION);
            }
        }
        public ThuTrongNgay()
        {
            InitializeComponent();
        }

        private void RefresForm(string p)
        {
            txtTienThu.EditValue = p;
            txtCongNo.EditValue = p;
            txtCT.EditValue = p;
            txtTenKH.EditValue = p;
            txtTienDaTra.EditValue = p;

            txtTongTien.EditValue = p;

            LoadCongNoTrongNgay();
        }

        private void EnabledForm(bool p)
        {
            txtTienThu.Enabled = p;
            if (frmMain.__khoaSo)
            {
                btnThem.Enabled = false;
            }
            else
            {

                btnThem.Enabled = !p;
            }
            btnLuu.Enabled = false;
            btnBoQua.Enabled = false;
            //     btnXoa.Enabled = true;

            txtCT.Properties.ReadOnly = true;
            txtTenKH.Properties.ReadOnly = true;
            txtTongTien.Properties.ReadOnly = true;
            txtTienDaTra.Properties.ReadOnly = true;
            txtCongNo.Properties.ReadOnly = true;
            txtTienThu.Properties.ReadOnly = !p;


        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            EnabledForm(true);
            btnThem.Enabled = false;
            btnLuu.Enabled = true;
            btnBoQua.Enabled = true;
            LoadThuInf();
            txtTienThu.Focus();
        }

        protected void LoadCongNoTrongNgay()
        {
            BUS.GIAODICHControl ctr = new BUS.GIAODICHControl();
            DataTable dt = ctr.GetALLByCT(null, "BH001", frmMain.__ngayMoSo, ref sErr);
            gridGiaoDich.DataSource = dt;
        }

        private void ThuTrongNgay_Load(object sender, EventArgs e)
        {
            EnabledForm(false);
            RefresForm("");
            SetPermission();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            RefresForm("");
            EnabledForm(false);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            sErr = SaveCongNo();
            if (sErr != "")
                XtraMessageBox.Show(sErr);
            else
            {
                RefresForm("");
                EnabledForm(false);
                XtraMessageBox.Show("Lưu thành công");
            }

        }
        protected String SaveCongNo()
        {
            if (inf.SOCT != "")
            {
                BUS.CONGNOControl ctr = new BUS.CONGNOControl();
                DTO.CONGNOInfo congnoinf = new DTO.CONGNOInfo();

                congnoinf.SOCT = inf.SOCT;
                congnoinf.NGAYCT = inf.NGAYCT;
                congnoinf.DIACHIKH = inf.DIACHIKH;
                congnoinf.DIENTHOAI = inf.DIENTHOAI;
                congnoinf.PHAITHU = false;
                congnoinf.D_C = "N";
                congnoinf.SOTIEN = Convert.ToDouble(txtTienThu.EditValue) * (-1);
                congnoinf.NOIDUNG = "Khách hàng trả trước";
                congnoinf.MAKH = inf.MAKH;
                congnoinf.SOCTTT = "BH";
                congnoinf.CREATEBY = frmMain.__userID;
                congnoinf.MODIFIEDBY = frmMain.__userID;
                congnoinf.CREATEDATE = frmMain.__ngayMoSo;
                congnoinf.MODIFIEDDATE = frmMain.__ngayMoSo;

                ctr.Add(congnoinf, ref sErr);

                if (sErr == "")
                {

                    inf.THUNGAY = inf.THUNGAY + Convert.ToDouble(txtTienThu.EditValue);
                    inf.TIENMAT = inf.THUNGAY;
                    inf.CONGNO = Convert.ToDouble(txtTongTien.EditValue) - Convert.ToDouble(inf.THUNGAY);

                    BUS.GIAODICHControl gdctr = new BUS.GIAODICHControl();
                    gdctr.Update(inf);
                }

            }
            else
                sErr = "Chưa chọn chứng từ nào.";
            return sErr;
        }

        private void txtTienThu_EditValueChanged(object sender, EventArgs e)
        {
            if (txtTienThu.Text != "")
                if (Convert.ToDouble(txtTienThu.EditValue) > (Convert.ToDouble(txtTongTien.EditValue) - Convert.ToDouble(txtTienDaTra.EditValue)))
                {

                    XtraMessageBox.Show("Vượt quá số tiền thiếu ");
                    txtTienThu.EditValue = "0";
                }
        }

        private void gridView_Click(object sender, EventArgs e)
        {
            if (btnThem.Enabled == false)
            {

                LoadThuInf();
                txtTienThu.Focus();
            }

        }

        private void LoadThuInf()
        {
            if (gridGiaoDich.CurrentRow != null && gridGiaoDich.CurrentRow.DataRow is DataRowView)
            {
                inf = new DTO.GIAODICHInfo(((DataRowView)gridGiaoDich.CurrentRow.DataRow).Row);

                txtCT.EditValue = "Số CT: " + inf.SOCT;
                txtTenKH.EditValue = "Tên khách hàng: " + ((DataRowView)gridGiaoDich.CurrentRow.DataRow).Row["TENKH"].ToString();
                txtTongTien.EditValue = inf.TONGTIEN;
                txtCongNo.EditValue = inf.CONGNO;
                txtTienDaTra.EditValue = inf.THUNGAY;
                txtTienThu.EditValue = 0;
            }
        }

        private void txtCongNo_ValueChanged(object sender, EventArgs e)
        {
            txtTienThu.Properties.MaxValue = txtCongNo.Value;
        }

        private void btnLamTuoi_Click(object sender, EventArgs e)
        {
            ThuTrongNgay_Load(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (gridGiaoDich.CurrentRow != null && gridGiaoDich.CurrentRow.DataRow is DataRowView)
            {
                inf = new DTO.GIAODICHInfo(((DataRowView)gridGiaoDich.CurrentRow.DataRow).Row);

                if (XtraMessageBox.Show("Bạn có thực sự muốn xóa", "Thông báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    //     sErr = DeleteHeader(inf.SOCT,inf.NGAYCT);

                    //sErr = DeleteDetail(inf.SOCT, inf.NGAYCT);
                    //if (sErr == "")
                    //{
                    sErr = DeleteCongNo(inf.SOCT, inf.NGAYCT);
                    //}

                    if (sErr != "")
                        XtraMessageBox.Show(sErr);
                    else
                    {
                        RefresForm("");
                        EnabledForm(false);
                    }
                }
            }
        }

        protected String DeleteHeader(String ct, DateTime ngay)
        {
            BUS.GIAODICHControl ctr = new BUS.GIAODICHControl();
            sErr = ctr.Delete(ct, "BH001", ngay);

            return sErr;
        }

        protected String DeleteDetail(String ct, DateTime ngay)
        {
            BUS.TONKHOControl tonkhoctr = new BUS.TONKHOControl();
            BUS.LOAIHANGControl lhctr = new BUS.LOAIHANGControl();
            BUS.NXHANGControl ctr = new BUS.NXHANGControl();
            DataTable dt = ctr.GetByAllByCT(ct, ngay, "BH001", ref sErr);

            foreach (DataRow row in dt.Rows)
            {
                DTO.NXHANGInfo inf = new DTO.NXHANGInfo(row);
                sErr = ctr.Delete(ct, inf.LINE, inf.LOAICT, inf.NGAYCT);
                if (sErr == "")
                {
                    DTO.LOAIHANGInfo lhinf = lhctr.Get(inf.LOAIHANG, ref sErr);
                    DTO.TONKHOInfo tkinf = new DTO.TONKHOInfo();
                    tkinf = tonkhoctr.Get(lhinf.NHOMHANG, lhinf.MAHANG, ref sErr);
                    tkinf.SOLUONG = tkinf.SOLUONG + inf.SOLUONG;
                    tkinf.SOTIEN = tkinf.SOLUONG * tkinf.GIAVON;
                    tonkhoctr.Update(tkinf);
                }
            }

            return sErr;
        }

        protected String DeleteCongNo(String ct, DateTime ngay)
        {
            BUS.CONGNOControl ctr = new BUS.CONGNOControl();
            DataTable dt = ctr.GetALLByCT(ct, ngay, ref sErr);
            DataRow[] rows = dt.Select("SOTIEN < 0");


            foreach (DataRow row in rows)
            {
                DTO.CONGNOInfo inf = new DTO.CONGNOInfo(row);
                sErr = ctr.Delete(inf.ID);

                if (sErr == "")
                {
                    BUS.GIAODICHControl gdctr = new BUS.GIAODICHControl();
                    DTO.GIAODICHInfo gdinf = gdctr.Get(ct, "BH001", ngay, ref sErr);
                    gdinf.THUNGAY = gdinf.THUNGAY - inf.SOTIEN * (-1);
                    gdinf.TIENMAT = gdinf.THUNGAY;
                    gdinf.CONGNO = gdinf.CONGNO + inf.SOTIEN * (-1);
                    gdctr.Update(gdinf);
                }

            }

            return sErr;
        }

        private void ThuTrongNgay_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (btnBoQua.Enabled)
                    btnBoQua_Click(null, null);

            }
            else if (e.KeyCode == Keys.F7)
            {
                if (btnLuu.Enabled)
                    btnLuu_Click(null, null);

            }
        }
    }
}